package com.sanji.sjmallmodule.goods;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.sanji.sjmallapp.MallApplication;
import com.sanji.sjmallapp.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by fhm on 2016/12/23.
 *
 */

public class SearchTypeAdapter extends BaseAdapter {
    private Context context;
    private List<SearchTypeBean> mData;
    private int clickTemp = -1;

    //标识选中的item
    public void setSelection(int position) {
        clickTemp = position;
    }

    public SearchTypeAdapter(Context context, List<SearchTypeBean> mData) {
        this.context = context;
        this.mData = mData;

    }

    public void notifyAdapter(List<SearchTypeBean> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vHolder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.fragment_search_grideview_item, null);
            vHolder = new ViewHolder(view);
            view.setTag(vHolder);
        } else {
            vHolder = (ViewHolder) view.getTag();
        }
        //点击
        // 改变选中listitem的背景颜色
        if (clickTemp == i) {
            vHolder.typeName.setBackgroundResource(R.drawable.search_text_select_rectangle);

            vHolder.typeName.setTextColor(context.getResources().getColor(R.color.colorWhite));
        } else {
            vHolder.typeName.setBackgroundResource(R.drawable.search_text_unselect_rectangle);
            vHolder.typeName.setTextColor(context.getResources().getColor(R.color.color_7f7f7f));
        }
        SearchTypeBean searchBean = mData.get(i);
        if (searchBean.getLineType() == 0) {
            vHolder.typeName.setLines(2);
        } else {
            vHolder.typeName.setLines(1);
        }
        vHolder.typeName.setText(searchBean.getSearchTypeName());
        //绑定tag
        vHolder.typeName.setTag(R.id.tag_first, i);
        vHolder.typeName.setTag(R.id.tag_second, view);
        return view;
    }

    class ViewHolder {
        @BindView(R.id.search_type_tv)
        TextView typeName;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
